import requests
import re
import os
import webbrowser
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept - encoding': 'gzip, deflate',
    'accept - language': 'zh - CN, zh;q = 0.9',
    'cache - control': 'no - cache',
    'Connection': 'keep-alive',
    'csrf': 'HH3GHIQ0RYM',
    'Referer': 'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/99.0.4844.51 Safari/537.36',
    'Cookie': '_ga=GA1.2.218753071.1648798611; _gid=GA1.2.144187149.1648798611; _gat=1; '
              'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1648798611; '
              'Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1648798611; kw_token=HH3GHIQ0RYM'
}

class MusicPlayer:
    def __init__(self, master):
        self.master = master
        master.title("酷我音乐播放器")

        # 搜索框
        self.search_frame = Frame(master)
        self.search_frame.pack(pady=10)
        self.search_label = Label(self.search_frame, text="输入歌手的名字或者想要搜索的歌曲：")
        self.search_label.pack(side=LEFT)
        self.search_entry = Entry(self.search_frame, width=30)
        self.search_entry.pack(side=LEFT)

        # 页数和起始页
        self.page_frame = Frame(master)
        self.page_frame.pack(pady=10)
        self.page_label = Label(self.page_frame, text="输入搜索的页数：")
        self.page_label.pack(side=LEFT)
        self.page_entry = Entry(self.page_frame, width=10)
        self.page_entry.pack(side=LEFT, padx=10)
        self.start_label = Label(self.page_frame, text="请输入起始页：")
        self.start_label.pack(side=LEFT)
        self.start_entry = Entry(self.page_frame, width=10)
        self.start_entry.pack(side=LEFT, padx=10)

        # 按钮
        self.button_frame = Frame(master)
        self.button_frame.pack(pady=10)
        self.search_button = Button(self.button_frame, text="搜索", command=self.search_music)
        self.search_button.pack(side=LEFT, padx=10)
        self.quit_button = Button(self.button_frame, text="退出", command=self.quit)
        self.quit_button.pack(side=LEFT, padx=10)

        # 音乐列表
        self.music_listbox = Listbox(master, width=100)
        self.music_listbox.pack()
        self.music_listbox.bind("<Double-Button-1>", self.play_music)

        # 进度条
        self.progressbar = ttk.Progressbar(master, orient=HORIZONTAL, length=200, mode='determinate')
        self.progressbar.pack()

        self.music_list = []

    def search_music(self):
        self.music_listbox.delete(0, END)
        self.music_list = []
        search_music = self.search_entry.get()
        start_page = int(self.start_entry.get())
        end_page = int(self.page_entry.get())
        self.progressbar['maximum'] = (end_page - start_page + 1) * 20
        for page in range(start_page, end_page + 1):
            url = 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn={}&rn=20&httpsStatus=1&reqId=11a3a510-d756-11ed-a189-415f05929f80'.format(
                search_music, page)
            response = requests.get(url, headers=headers).json()
            music_reqid = response['reqId']
            for music_data in response['data']['list']:
                music_name = music_data['name']
                music_artist = music_data['artist']
                music_rid = music_data['rid']
                self.music_list.append({'name': music_name, 'artist': music_artist, 'rid': music_rid, 'reqid': music_reqid})
                self.music_listbox.insert(END, music_name + ' - ' + music_artist)
                self.progressbar['value'] += 1
                self.master.update()

    def play_music(self, event):
        selected_indices = self.music_listbox.curselection()
        if not selected_indices:
            return
        i = selected_indices[0]
        music = self.music_list[i]
        music_rid = music['rid']

        
        # 在线播放链接
        play_url = 'http://www.kuwo.cn/play_detail/{}'.format(music_rid)
        webbrowser.open_new_tab(play_url)

    def quit(self):
        self.master.quit()
        self.master.destroy()

root = Tk()
app = MusicPlayer(root)
root.protocol("WM_DELETE_WINDOW", app.quit)
root.mainloop()
